import http from '@/plugins/axios'

/*1.用浏览器内部转换器实现html转码*/
export function htmlEncode (html){
  //1.首先动态创建一个容器标签元素，如DIV
  var temp = document.createElement ("div");
  //2.然后将要转换的字符串设置为这个元素的innerText(ie支持)或者textContent(火狐，google支持)
  (temp.textContent != undefined ) ? (temp.textContent = html) : (temp.innerText = html);
  //3.最后返回这个元素的innerHTML，即得到经过HTML编码转换的字符串了
  var output = temp.innerHTML;
  temp = null;
  return output;
}

/*2.用浏览器内部转换器实现html解码*/
export function htmlDecode (text){
  //1.首先动态创建一个容器标签元素，如DIV
  var temp = document.createElement("div");
  //2.然后将要转换的字符串设置为这个元素的innerHTML(ie，火狐，google都支持)
  temp.innerHTML = text;
  //3.最后返回这个元素的innerText(ie支持)或者textContent(火狐，google支持)，即得到经过HTML解码的字符串了。
  var output = temp.innerText || temp.textContent;
  temp = null;
  return output;
}

export function getAllDepartment() {
  return http({
    method: 'POST',
    url: '/ykf/dictDetail/listByNo',
    data: { dictNo: 'department' }
  })
}

export function getLibraryManagePage(data) {
  return http({
    method: 'POST',
    url: '/ykf/libraryManage/page',
    data
  })
}

export function getLibraryManageDetail(data) {
  return http({
    method: 'POST',
    url: '/ykf/libraryManage/info',
    data
  })
}

export function addLibraryManage(data) {
  return http({
    method: 'POST',
    url: '/ykf/libraryManage/save',
    data: {
      ...data,
      content: data.content ? htmlEncode(data.content) : ''
    }
  })
}

export function updateLibraryManage(data) {
  return http({
    method: 'POST',
    url: '/ykf/libraryManage/update',
    data: {
      ...data,
      content: data.content ? htmlEncode(data.content) : ''
    }
  })
}

export function removeLibraryManage(data) {
  return http({
    method: 'POST',
    url: '/ykf/libraryManage/delete',
    data
  })
}
